class Player
  attr_reader :name, :game, :strategy
  
  def initialize name
    @name = name
  end
  
  def play    
    begin
      puts "\n#{@name} using strategy=#{@strategy}"
      eval @strategy unless @strategy.include? "exit"
      notify_achievements
    rescue
      @strategy = @historical_strategy      
      game.set_status @name, "Error in new strategy. Using earlier one"
      puts "rescued for " + $!.message
      eval @strategy
    end  
  end
  
  def notify_achievements
    @game.add_achievement "#{@name} reached Population Limit" if my_farmers + my_warriors >= 200
    @game.add_achievement "#{@name} reached Resource Limit" if my_resources >= 50000
  end
  
  def game= game
    @game = game
  end
  
  def status
    @game.get_status @name
  end

  def update_strategy strategy
    @historical_strategy = @strategy
    @strategy = strategy
    @game.clear_warning @name
  end
  
  def my_farmers
    @game.get_farmers @name   
  end
  
  def my_warriors
    @game.get_warriors @name   
  end
  
  def my_resources
    @game.get_resources @name   
  end
  
  def strongest_player
    @game.strongest
  end
  
  def create_farmer
    @game.create_farmer @name
  end
  
  def farm    
    @game.farm @name
  end
  
  def create_warrior
    @game.create_warrior @name
  end

  def build_barrack
    @game.build_barrack @name
  end
  
  def attack player_name
    puts "attacking #{player_name}"
    @game.attack @name, player_name    
  end
  
  def gift_warriors receiver_name, count
    @game.gift_army @name, receiver_name, count
  end

  def gift_resources receiver_name, count
    @game.gift_resources @name, receiver_name, count
  end
end